package com.xgggh.redpack;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.jakewharton.rxbinding2.support.design.widget.RxNavigationView;
import com.jakewharton.rxbinding2.view.RxView;
import com.xgggh.app.RxBaseActivity;
import com.xgggh.binding.data.home.ActivityHome;
import com.xgggh.redpack.databinding.ActivityHomeBinding;
import com.xgggh.redpack.databinding.NavHeaderHomeBinding;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;

/**
 * @title 红包首页
 */
public class HomeActivity extends RxBaseActivity {
    private static final int camera_request_code=0x112;
    private static final int sponsor_request_code=0x113;


    //主页
    ActivityHomeBinding mBinding;
    //侧边栏
    NavHeaderHomeBinding mNavHeaderHomeBinding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding=DataBindingUtil.setContentView(this,R.layout.activity_home);
        mBinding.setActivityHome(new ActivityHome());
        setSupportActionBar(mBinding.appBarHome.toolBarParent.toolbar);
        //添加侧边栏UI
        mNavHeaderHomeBinding= DataBindingUtil.inflate(getLayoutInflater(),R.layout.nav_header_home,mBinding.navView,false);
        mBinding.navView.addHeaderView(mNavHeaderHomeBinding.getRoot());

        onCreateListener();
    }


    private void onCreateListener(){
        //赞助红包
        RxView.clicks(mBinding.appBarHome.fab)
                .throttleFirst(windowDuration, TimeUnit.SECONDS)
                .subscribeOn(AndroidSchedulers.mainThread())
                .compose(bindToLifecycle())
                .subscribe(o -> sponsorActivity());

        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, mBinding.drawerLayout, mBinding.appBarHome.toolBarParent.toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        mBinding.drawerLayout.addDrawerListener(toggle);
        toggle.syncState();

        //侧边栏事件
        RxNavigationView.itemSelections(mBinding.navView)
                .throttleFirst(windowDuration, TimeUnit.SECONDS)
                .subscribeOn(AndroidSchedulers.mainThread())
                .compose(bindToLifecycle())
                .subscribe(menuItem -> onNavigationItemSelected(menuItem));

        RxView.clicks(mNavHeaderHomeBinding.navigationHeader)
                .throttleFirst(windowDuration, TimeUnit.SECONDS)
                .subscribeOn(AndroidSchedulers.mainThread())
                .compose(bindToLifecycle())
                .subscribe(object -> profileActivity());
        //个人主页
        Observable header= RxView.clicks(mBinding.appBarHome.contentHome.header);
        Observable nickName= RxView.clicks(mBinding.appBarHome.contentHome.nickName);
        Observable redPackInfo= RxView.clicks(mBinding.appBarHome.contentHome.redPackInfo);
        Observable.merge(header,nickName,redPackInfo)
                .throttleFirst(windowDuration, TimeUnit.SECONDS)
                .subscribeOn(AndroidSchedulers.mainThread())
                .compose(bindToLifecycle())
                .subscribe(object -> homePageActivity());
        //评价
        RxView.clicks(mBinding.appBarHome.contentHome.comment)
                .throttleFirst(windowDuration, TimeUnit.SECONDS)
                .subscribeOn(AndroidSchedulers.mainThread())
                .compose(bindToLifecycle())
                .subscribe(object -> commentActivity());
        //jubao
        RxView.clicks(mBinding.appBarHome.contentHome.report)
                .throttleFirst(windowDuration, TimeUnit.SECONDS)
                .subscribeOn(AndroidSchedulers.mainThread())
                .compose(bindToLifecycle())
                .subscribe(object -> reportActivity());

        //活动公告
        RxView.clicks(mBinding.appBarHome.contentHome.notice)
                .throttleFirst(windowDuration, TimeUnit.SECONDS)
                .subscribeOn(AndroidSchedulers.mainThread())
                .compose(bindToLifecycle())
                .subscribe(object -> noticeActivity());

        RxView.clicks(mBinding.appBarHome.contentHome.openRedPack)
                .throttleFirst(windowDuration, TimeUnit.SECONDS)
                .subscribeOn(AndroidSchedulers.mainThread())
                .compose(bindToLifecycle())
                .subscribe(object -> openRedPackActivity());
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
            if (resultCode==RESULT_OK && requestCode==camera_request_code){

            }else if (resultCode==RESULT_OK && requestCode==sponsor_request_code){
                Toast.makeText(this, "派发红包成功", Toast.LENGTH_SHORT).show();
            }
    }

    private void openRedPackActivity(){
        Intent intent = new Intent(this,SpaActivity.class);
        startActivity(intent);
    }


    private void noticeActivity(){
        Intent intent = new Intent(this,NoticeActivity.class);
        startActivity(intent);
    }
    private void reportActivity(){
        Intent intent = new Intent(this,ReportActivity.class);
        startActivity(intent);
    }

    private void commentActivity(){
        Intent intent = new Intent(this,CommentActivity.class);
        startActivity(intent);
    }

    //个人主页
    private void homePageActivity(){
        Intent intent = new Intent(this,HomePageActivity.class);
        startActivity(intent);
    }

    //个人资料
    private void profileActivity(){
        Intent intent = new Intent(this,ProfileActivity.class);
        startActivity(intent);
        closeDrawer(mBinding.drawerLayout);
    }

    //赞助红包
    private void sponsorActivity(){
        Intent intent = new Intent(this,SponsorActivity.class);
        startActivityForResult(intent,sponsor_request_code);
    }


    //关闭侧边栏
    private void  closeDrawer(DrawerLayout drawer){
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        }
    }


    @Override
    public void onBackPressed() {
        if (mBinding.drawerLayout.isDrawerOpen(GravityCompat.START)) {
            mBinding.drawerLayout.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.home, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_camera) {
            startActivityForResult(new Intent(this,CameraActivity.class),camera_request_code);
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();
        if (id == R.id.nav_camera) {
            myRedPackActivity();
        } else if (id == R.id.nav_gallery) {
            walletActivity();
        } else if (id == R.id.nav_slideshow) {
            leaderBoardActivity();
        } else if (id == R.id.nav_manage) {
            toolsActivity();
        } else if (id == R.id.nav_share) {
            shareActivity();
        } else if (id == R.id.nav_send) {
            sendActivity();
        }
        closeDrawer(mBinding.drawerLayout);
        return true;
    }


    //排行榜
    private void leaderBoardActivity(){
        Intent intent = new Intent(this,LeaderBoardActivity.class);
        startActivity(intent);
    }


    //我的钱包
    private void walletActivity(){
        Intent intent = new Intent(this,WalletActivity.class);
        startActivity(intent);
    }


    //我的红包
    private void myRedPackActivity(){
        Intent intent = new Intent(this,MyRedPackActivity.class);
        startActivity(intent);
    }


    //我的设置
    private void toolsActivity(){
        Intent intent = new Intent(this,ToolsActivity.class);
        startActivity(intent);
    }


    //分享 微信/支付宝/新浪微博等
    private void shareActivity(){
        Intent intent = new Intent(this,ShareActivity.class);
        startActivity(intent);
    }


    //推荐 发送本app给好友，无流量发送
    //生成二维码，发送给好友。可以通过扫描二维码下载
    private void sendActivity(){
        Intent intent = new Intent(this,SendActivity.class);
        startActivity(intent);
    }

}
